﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

/// <summary>
/// 文件上传接口
/// </summary>
public class IFileUp
{
    public IFileUp()
    {
        
    }

    /// <summary>
    /// 文件类型
    /// </summary>
    public string fileType = string.Empty;
    /// <summary>
    /// 文件大小(KB)
    /// </summary>
    public int fileSize = 0;
    /// <summary>
    /// 上传后大小(KB)
    /// </summary>
    public int serverSize = 0;
    /// <summary>
    /// 保存虚拟根目录
    /// </summary>
    public string saveDir = "/";
    /// <summary>
    /// 错误信息
    /// </summary>
    public string errorInfo = string.Empty;
    /// <summary>
    /// 上传后路径
    /// </summary>
    public string serverPath = string.Empty;
    /// <summary>
    /// 是否加入日期目录
    /// </summary>
    public bool IsDateDir = true;

    /// <summary>
    /// 验证文件类型
    /// </summary>
    /// <param name="ExtensionName">扩展名</param>
    /// <returns></returns>
    protected bool ValidateFileType(string ExtensionName)
    {
        ExtensionName = ExtensionName.ToLower();
        if (fileType == "*") { return true; }
        string[] extenarray = fileType.ToLower().Split('|');
        foreach (string temp in extenarray)
        {
            if ("." + temp == ExtensionName)
            {
                return true;
            }
        }
        return false;
    }
    /// <summary>
    /// 验证文件大小
    /// </summary>
    /// <param name="ContentLength">文件长度</param>
    /// <returns></returns>
    protected bool ValidateFileSize(int ContentLength)
    {
        if (ContentLength != 0)
        {
            serverSize = ContentLength / 1024;
            if (serverSize <= fileSize)
            {
                return true;
            }
            return false;
        }
        return true;
    }

    /// <summary>
    /// 开始上传
    /// </summary>
    /// <param name="fup_up">表单里的上传控件</param>
    /// <returns>是否成功</returns>
    public bool UpLoad(FileUpload fup_up)
    {
        if (fup_up.HasFile)
        {
            //验证文件类型
            string fileExten = new System.IO.FileInfo(fup_up.FileName).Extension;
            if (!ValidateFileType(fileExten))
            {
                errorInfo = "文件应该为：" + fileType;
                return false;
            }
            //验证文件大小
            if (!ValidateFileSize(fup_up.PostedFile.ContentLength))
            {
                errorInfo = "文件应小于：" + fileSize.ToString() + "K";
                return false;
            }

            string uploadDir = saveDir;
            string fullName = string.Empty;
            string GetTimeFileName = Guid.NewGuid().ToString();

            DateTime t = DateTime.Now;
            if (IsDateDir)
            {
                uploadDir += string.Format("{0}/{1}/{2}/", t.Year.ToString(), t.Month.ToString(), t.Day.ToString());
            }
            //组合新文件名
            fullName = uploadDir + GetTimeFileName + fileExten.ToLower();

            //获取虚拟目录
            //string virdir = UIMethod.ApplicationPath();
            //string upload = UIMethod.GetAppSettings("ImagesDNS");
            string imgdir = HttpContext.Current.Request.MapPath("~/");

            //设置临时存放地址
            //string tempPath = imgdir + GetTimeFileName + fup_up.FileName;
            //设置新地址
            string savePath = imgdir + fullName;
            //创建文件夹
            TXIO.Txt_IO.CreateDirectory(imgdir + uploadDir);
            //显示相对全路径
            serverPath = fullName.ToString();

            //保存文件
            fup_up.PostedFile.SaveAs(savePath);
        }
        else
        {
            errorInfo = "请选择一个本地文件";
            return false;
        }

        return true;
    }
}
